package com.ailk.tcm.hffw.android.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.media.SoundPool;
import android.util.Log;

public class MyAudioManager {
	private static SoundPool soundPool = new SoundPool(2,
			AudioManager.STREAM_MUSIC, 0);;
	private static Map<String, Integer> soundMap = new HashMap<String, Integer>();

	public static void loadSound(Context context, String soundName, int resId) {
		soundMap.put(soundName, soundPool.load(context, resId, 1));
	}

	public static void playMusic(String soundName) {
		try {
			soundPool.play(soundMap.get(soundName), 10, 10, 0, 0, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void recycle() {
		soundPool.release();
	}

	public static MediaRecorder startRecording(String outputFilePath) {
		MediaRecorder mRecorder = new MediaRecorder();
		// 设置音源为Micphone
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		// 设置封装格式
		mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mRecorder.setOutputFile(outputFilePath);
		// 设置编码格式
		mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

		try {
			mRecorder.prepare();
		} catch (IOException e) {
			Log.e(MyAudioManager.class.getName(), "record prepare failed");
		}
		mRecorder.start();
		return mRecorder;
	}

	public static void stopRecording(MediaRecorder mRecorder) {
		mRecorder.stop();
		mRecorder.release();
	}
}
